
<template>
  <div class="icon-body">
    <a-input v-model="name" allowClear placeholder="请输入图标名称" @change="filterIcons">
      <i slot="suffix" />
    </a-input>
    <div class="icon-list">
      <div v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
        <a-icon :type="item.name" style="height: 30px;width: 16px;vertical-align: middle;"
          ref="icon" />
        <span
          style="text-overflow: ellipsis; white-space: nowrap; width: 100px;">{{ item.name}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import * as VueIcon from '@ant-design/icons/lib'
export default {
  name: 'IconSelect',
  data () {
    return {
      name: '',
      icons: [],
      iconList: []
    }
  },
  methods: {
    filterIcons () {
      this.iconList = this.icons
      if (this.name) {
        this.iconList = this.iconList.filter(item => item.name.includes(this.name))
      }
    },
    selectedIcon (icon) {
      this.$emit('selected', icon)
      document.body.click()
    },
    getType (item) {
      let name = item.displayName
      function replacer (match) {
        return '-' + match.toLowerCase()
      }
      if (name) {
        let type = name.replace(/([A-Z])/g, replacer)
        let newType = type.split('-').slice(1, -1).join('-')

        return newType
      }
    },
    getIconList (data) {
      for (let key in data) {
        if (data[key].theme === 'outline') {
          this.icons.push(data[key])
        }
      }
      return this.icons
    }
  },
  mounted () {

  },
  created () {
    this.iconList = this.getIconList(VueIcon)
  }
}
</script>

<style  scoped>
.icon-body {
  width: 100%;
  padding: 10px;
}
.icon-body .icon-list {
  height: 200px;
  overflow-y: scroll;
}
.icon-body .icon-list div {
  height: 30px;
  line-height: 30px;
  margin-bottom: -5px;
  cursor: pointer;
  width: 33%;
  float: left;
}
.icon-body .icon-list span {
  display: inline-block;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>
